'use strict';

define(function(require,exports,module){
	
	var React = require('react');
	var $ = require('jquery');
	var moment = require('moment');

	var publicObj = require('lib/public');
	var token = publicObj.get_token();
	
	/*DATE下拉框*/
	var SelectOptionDate = require('components/partial/SelectOptionDate');
	
	//折线图
	var AccessLogLineDiagram = require('components/diagram/DayOpenLineDiagram');

    var AccessLogMuliti = require('components/diagram/SplineDiagram/AccessLogMuliti');

    //TimeBucket
    var AccessLogTimeBucket = require('./AccessLogDiagram/TimeBucket');
	
	module.exports = React.createClass({displayName: "exports",
		handleDateChange:function(event){
			var date = event.target.value;
			this.setState({'date':date});
            this.setState({'hardware':''});
			this.loadData(date,'');
		},
		handleHardwareChange:function(event){
			var hardware = event.target.value;
			this.setState({'hardware':hardware});
			this.loadData(this.state.date,hardware);
		},
		loadData:function(date,hardware){
			
			this.setState({data:[]});
			if(date){

                var sql;

                sql = "SELECT count(*) as value,\
							thehour as time\
							FROM qeeniao.access_log\
							where thedate = "+date+" \
							group by time\
							order by time ASC";

				$.ajax({
					url:this.props.url,
					data:{sql:sql,'access_token':token},
					type:'post',
					dataType:'json',
					success:function(o){
						var tmp = [];
						tmp.push(o);
						if(o){
							if(this.isMounted()) {
								this.setState({'data': tmp});
							}
						}
					}.bind(this),
					error:function(xhr,status,err){
						console.error(sql,status,err.toString());
					}.bind(this)
				});
			}
		},
		getInitialState:function(){
			return {
				'data':[],
                'all_data':{'ios':[],'android':[],'all':[]},
				'date':'',
				'ajax_data':{'access_token':token}
			}
		},
		render:function(){
            var diagram;

            if(this.state.data.length > 0){
                diagram = this.state.data.map(function(item){
                    return (
                        React.createElement(AccessLogLineDiagram, {className: "", id: "AccessLog", id_name: "AccessLog", title: "单日统计次数", yName: "统计次数", yUnit: "（时/次数）", url: "", data: item})
                    );
                });
            }else{
				diagram = React.createElement(AccessLogMuliti, {className: "", id: "AccessLogAreaBasic", id_name: "AccessLogAreaBasic", y: "统计次数", title: "每日统计次数", ajax_data: this.state.ajax_data, url: this.props.url, interval: "300000"});
            }


			return (
				React.createElement("div", {className: "col-xs-6"}, 
					React.createElement("div", {className: "row bgwhite"}, 
						React.createElement("div", {className: "col-xs-4 bs-callout bs-callout-info"}, 
							React.createElement("h4", null, this.props.title, ": ")
						), 
						React.createElement("div", {className: "col-xs-4 "}, 
							React.createElement(SelectOptionDate, {handleDateChange: this.handleDateChange, data: this.props.data})
						), 
						React.createElement("div", {className: "col-xs-2", style: {margin:'4px 0'}}, 
							React.createElement("button", {onClick: this.createHandle, className: "btn btn-primary btn", "data-toggle": "modal", "data-target": "#AccessLogTimeBucket"}, "更多")
						), 
                        React.createElement(AccessLogTimeBucket, {id: "AccessLogTimeBucket", url: this.props.url})
					), 
					React.createElement("div", {className: "row"}, 
					diagram
					)
				)
			);
		}
	});

});